<template>
  <div class="main" :class="{'main-hide-text': bShrink}">
    <div class="sidebar-menu-con" :style="{width: bShrink?'60px':'200px', overflow: bShrink ? 'visible' : 'auto'}">
      <shrinkable-menu
        :shrink="bShrink"
        @on-change="handleSubmenuChange"
        :theme="theme"
        :before-push="beforePush"
        :menu-list="menuList">
        <div slot="top" class="logo-con">
          <img v-show="!bShrink" src="../assets/images/logo.png" key="max-logo"/>
        </div>
      </shrinkable-menu>
    </div>
    <div class="main-header-con" :style="{paddingLeft: bShrink?'60px':'200px'}">
      <div class="main-header">
        <div class="header-middle-con">
          <div class="main-breadcrumb">
            <breadcrumb-nav :currentPath="currentPath"></breadcrumb-nav>
          </div>
        </div>
        <div class="header-avator-con">
          <div class="user-dropdown-menu-con">
            <Row type="flex" justify="end" align="middle" class="user-dropdown-innercon">
              <Dropdown transfer trigger="click" @on-click="handleClickUserDropdown" v-if="currentUser">
                <a href="javascript:void(0)">
                  <span class="main-user-name">{{loginName}}</span>
                  <Icon type="arrow-down-b"></Icon>
                </a>
                <DropdownMenu slot="list">
                  <router-link :to="{ path: '/index/system/home'}">
                    <DropdownItem name="personage" divided>个人中心</DropdownItem>
                  </router-link>
                  <DropdownItem name="loginout" @click="loginOut" divided>退出登录</DropdownItem>
                </DropdownMenu>
              </Dropdown>
              <div v-if="currentUser.headPhotoUrl" class="personage-img"><img
                :src="currentUser.imgHeadUrl + currentUser.headPhotoUrl"/></div>
              <Avatar icon="person" style="background: #619fe7;margin-left:10px;"
                      v-if="!currentUser.headPhotoUrl">
              </Avatar>
              <Dropdown transfer trigger="click" @on-click="handleClickUserDropdown"
                        v-if="currentUser == null">
                <span @click="loginTo" class="login-word">登录</span>
              </Dropdown>

            </Row>
          </div>
        </div>
      </div>
    </div>
    <div class="single-page-con" :style="{left: bShrink?'60px':'200px'}">
      <div class="single-page">
        <router-view></router-view>
      </div>
    </div>
  </div>
</template>
<script>
  import Vue from 'vue'
  import Cookies from 'js-cookie';
  import util from '@/libs/util.js';
  import shrinkableMenu from './common/shrinkable-menu.vue';
  import breadcrumbNav from './common/breadcrumb-nav.vue';
  import {SERVICE_URLS} from "../libs/service.url";

  export default {
    components: {
      shrinkableMenu,
      breadcrumbNav
    },
    data() {
      return {
        bShrink: false,
        theme: 'dark',
        loginName: '',
        menuList : [
          {
            url: "/culture",
            privilegeName: "百年晋小",
            childPrivilege : [
              {
                privilegeName: "关于晋阳",
                url: "/culture/about",
                childPrivilege : []
              },
              {
                privilegeName: "和谐团队",
                url: "/culture/teams",
                childPrivilege : []
              },
              {
                privilegeName: "荣誉展示",
                url: "/culture/honor",
                childPrivilege : []
              }
            ]
          },
          {
            url: "/teachers",
            privilegeName: "师资力量",
            childPrivilege : [
              {
                privilegeName: "老师风采",
                url: "/teachers/detail",
                childPrivilege : []
              },
              {
                privilegeName: "教研活动",
                url: "/teachers/teach-activity",
                childPrivilege : []
              },              {
                privilegeName: "工会活动",
                url: "/teachers/union-activity",
                childPrivilege : []
              }
            ]
          },
          {
            url: "/student",
            privilegeName: "向阳花们",
            childPrivilege : [
              {
                privilegeName: "学生活动",
                url: "/student/activity",
                childPrivilege : []
              },
              {
                privilegeName: "童心飞扬",
                url: "/student/craftsmanship",
                childPrivilege : []
              },              {
                privilegeName: "荣誉之花",
                url: "/student/flower",
                childPrivilege : []
              }
            ]
          },
          {
            url: "/campus",
            privilegeName: "校园新闻",
            childPrivilege : [
              {
                privilegeName: "校园公告",
                url: "/campus/notice",
                childPrivilege : []
              },
              {
                privilegeName: "校园新闻",
                url: "/campus/news",
                childPrivilege : []
              },              {
                privilegeName: "每周食谱",
                url: "/campus/cookbook",
                childPrivilege : []
              }
            ]
          },
          {
            url: "/live",
            privilegeName: "直播管理",
            childPrivilege : [
              {
                privilegeName: "在线直播",
                url: "/live/index",
                childPrivilege : []
              }
            ]
          },
          {
            url: "/mailbox",
            privilegeName: "校长信箱",
            childPrivilege : [
              {
                privilegeName: "留言管理",
                url: "/mailbox/words",
                childPrivilege : []
              }
            ]
          }
        ]
      }
    },
    computed: {
      currentPath() {
        return this.$store.state.app.currentPath; // 当前面包屑数组
      },
      currentUser() {
        return this.$store.getters.currentUser
      }
    },
    created() {
      if (this.currentUser != null) {
        this.loginName = this.currentUser.realName?this.currentUser.realName:this.currentUser.loginName;
      }
    },
    mounted() {
      this.init();
    },
    methods: {
      init() {
        util.setCurrentPath(this, this.$route.name);
      },
      toggleClick() {
        this.bShrink = !this.bShrink;
      },
      handleClickUserDropdown(name) {
        if (name === 'ownSpace') {
          util.openNewPage(this, 'ownspace_index');
          this.$router.push({
            name: 'ownspace_index'
          });
        } else if (name === 'loginout') {
          // 退出登录
          this.$store.commit('logout', this);
          this.$store.commit('clearOpenedSubmenu');
          this.$router.push({
            name: 'login'
          });
        }
      },
      checkTag(name) {
        let openpageHasTag = this.pageTagsList.some(item => {
          if (item.name === name) {
            return true;
          }
        });
        if (!openpageHasTag) { //  解决关闭当前标签后再点击回退按钮会退到当前页时没有标签的问题
          util.openNewPage(this, name, this.$route.params || {}, this.$route.query || {});
        }
      },
      handleSubmenuChange(val) {

      },
      beforePush(name) {
        // if (name === 'accesstest_index') {
        //     return false;
        // } else {
        //     return true;
        // }
        return true;
      },
      loginOut() {
        Vue.submit(this, {
          loading: 'loading',
          url: SERVICE_URLS.LoginManage.exit,
          params: {},
          callback: (_data) => {
            localStorage.clear();
            this.currentUser = null;
            this.$router.push({
              name: 'login'
            });
          }
        })
      },
      loginTo() {
        this.$router.push({
          name: 'login'
        });
      }
    },
    watch: {
      '$route'(to) {
        let pathArr = util.setCurrentPath(this, to.name);
      }
    }
  };
</script>
<style scoped lang="scss">
  .login-word {
    cursor: pointer;
  }

  .personage-img {
    width: 32px;
    height: 32px;
    overflow: hidden;
    border-radius: 50%;
    margin-left: 11px;
    float: right;
    img {
      width: 100%;
      height: 100%;
    }
  }
</style>
